export class SheepScore{
  /**
   sheepValue = 10; // 每只羊存活的基础分
   positionScore = 1; // 羊相邻的加分
   trapScore = 50; // 狼被困住的加分
   */
  evaluateSheepScore(board: number[][], sheepValue: number, positionScore: number, trapScore: number): number {
    let score = 0;

    // 羊的存活数量
    for (let row = 0; row < board.length; row++) {
      for (let col = 0; col < board[row].length; col++) {
        if (board[row][col] == 1) { // 假设1代表羊
          score += sheepValue;
        }
      }
    }

    // 羊的位置分布
    const directions = [[-1, 0], [1, 0], [0, -1], [0, 1]]; // 上下左右四个方向
    for (let row = 0; row < board.length; row++) {
      for (let col = 0; col < board[row].length; col++) {
        if (board[row][col] == 1) { // 假设1代表羊
          for (const [dx, dy] of directions) {
            const newRow = row + dx;
            const newCol = col + dy;
            if (newRow >= 0 && newRow < board.length && newCol >= 0 && newCol < board[row].length && board[newRow][newCol] == 1) {
              score += positionScore;
            }
          }
        }
      }
    }

    // 狼的活动范围
    for (let row = 0; row < board.length; row++) {
      for (let col = 0; col < board[row].length; col++) {
        if (board[row][col] == 2) { // 假设2代表狼
          let canMove = false;
          for (const [dx, dy] of directions) {
            const newRow = row + dx;
            const newCol = col + dy;
            if (newRow >= 0 && newRow < board.length && newCol >= 0 && newCol < board[row].length && (board[newRow][newCol] == 0 || (board[newRow][newCol] == 1 && this.checkValidMove(board, newRow, newCol)))) {
              canMove = true;
              break;
            }
          }
          if (!canMove) {
            score += trapScore;
          }
        }
      }
    }
    // 减去可能被吃掉的羊的分数
    score -= this.calculateEatenSheepScore(board, 100);
    // 根据羊包围狼的可能性调整得分
    score += this.calculateSurroundingProbabilityScore(board, sheepValue, trapScore);
    // 在evaluateSheepScore方法末尾添加以下逻辑
    score += this.calculatePairEatenPenalty(board, sheepValue);


    return score;
  }

// 检查狼是否可以移动到目标位置
  private checkValidMove(board: number[][], row: number, col: number): boolean {
    const directions = [[-1, 0], [1, 0], [0, -1], [0, 1]];
    for (const [dx, dy] of directions) {
      const newRow = row + dx * 2;
      const newCol = col + dy * 2;
      if (newRow >= 0 && newRow < board.length && newCol >= 0 && newCol < board[row].length && board[newRow][newCol] == 0) {
        return true;
      }
    }
    return false;
  }
// 计算可能被吃掉的羊的总分
  private calculateEatenSheepScore(board: number[][], sheepValue: number): number {
    let eatenScore = 0;

    // 遍历棋盘，找到所有可能被吃的羊
    for (let row = 0; row < board.length; row++) {
      for (let col = 0; col < board[row].length; col++) {
        if (board[row][col] == 1) { // 假设1代表羊
          // 检查是否有狼可以吃掉这只羊
          if (this.canBeEaten(board, row, col)) {
            eatenScore += sheepValue;
          }
        }
      }
    }

    return eatenScore;
  }

  // 检查羊是否可以被吃掉
  private canBeEaten(board: number[][], row: number, col: number): boolean {
    const directions = [[-1, 0], [1, 0], [0, -1], [0, 1]];
    for (const [dx, dy] of directions) {
      const newRow = row + dx * 2;
      const newCol = col + dy * 2;
      if (newRow >= 0 && newRow < board.length && newCol >= 0 && newCol < board[row].length && board[newRow][newCol] == 2) {
        return true;
      }
    }
    return false;
  }
  // 计算羊包围狼的可能性得分
  private calculateSurroundingProbabilityScore(board: number[][], sheepValue: number, trapScore: number): number {
    const directions = [[-1, 0], [1, 0], [0, -1], [0, 1]];
    let surroundingScore = 0;

    // 遍历棋盘，找到所有狼
    for (let row = 0; row < board.length; row++) {
      for (let col = 0; col < board[row].length; col++) {
        if (board[row][col] === 2) { // 假设2代表狼
          // 检查狼周围是否有足够的羊包围它
          const surroundingSheepCount = this.countSurroundingSheep(board, row, col);
          const probability = this.calculateSurroundingProbability(surroundingSheepCount);
          surroundingScore += probability * trapScore;
        }
      }
    }

    return surroundingScore;
  }

  // 计算羊包围狼的可能性
  private calculateSurroundingProbability(surroundingSheepCount: number): number {
    // 这里假设至少需要4只羊才能包围一只狼
    if (surroundingSheepCount >= 4) {
      // 假设每增加一只羊，包围的可能性增加10%
      return Math.min(1, 0.1 * (surroundingSheepCount - 3));
    }
    return 0;
  }

  // 计算狼周围羊的数量
  private countSurroundingSheep(board: number[][], wolfRow: number, wolfCol: number): number {
    const directions = [[-1, 0], [1, 0], [0, -1], [0, 1]];
    let count = 0;

    for (const [dx, dy] of directions) {
      const newRow = wolfRow + dx;
      const newCol = wolfCol + dy;
      if (newRow >= 0 && newRow < board.length && newCol >= 0 && newCol < board[wolfRow].length && board[newRow][newCol] === 1) {
        count++;
      }
    }

    return count;
  }
// 添加新的私有方法来计算因羊成对被吃导致的惩罚分
  private calculatePairEatenPenalty(board: number[][], sheepValue: number): number {
    let penalty = 0;
    const directions = [[-2, 0], [2, 0], [0, -2], [0, 2]]; // 检查两步距离，以识别可能同时被吃的羊

    // 遍历棋盘，寻找可能吃掉两只羊的狼位置
    for (let row = 0; row < board.length; row++) {
      for (let col = 0; col < board[row].length; col++) {
        if (board[row][col] !== 2) continue; // 跳过非狼位置

        let pairFound = false;
        for (const [dx, dy] of directions) {
          const firstNewRow = row + dx;
          const firstNewCol = col + dy;
          const secondNewRow = row - dx;
          const secondNewCol = col - dy;

          // 检查两个潜在羊的位置是否合理且为羊
          if (
            this.isValidPosition(board, firstNewRow, firstNewCol) && board[firstNewRow][firstNewCol] === 1 &&
            this.isValidPosition(board, secondNewRow, secondNewCol) && board[secondNewRow][secondNewCol] === 1
          ) {
            pairFound = true;
            break;
          }
        }

        if (pairFound) {
          penalty -= 10000; // 发现一对羊可被同一只狼吃到，扣分
        }
      }
    }

    return penalty;
  }

// 辅助方法检查给定位置是否在棋盘上
  private isValidPosition(board: number[][], row: number, col: number): boolean {
    return row >= 0 && row < board.length && col >= 0 && col < board[0].length;
  }
}
// let sheepScore=new SheepScore()
// let board=[
//   [0,0,2,0,0],
//   [0,0,1,0,0],
//   [1,2,1,2,1],
//   [1,1,0,1,1],
//   [1,1,1,1,1],
// ]
// let score=sheepScore.evaluateSheepScore(board,10,1,50)
// console.log(score)
